/**
 * Created by hda on 2017/6/1.
 */
define(['jquery', 'ks-spa', 'ks-verify'], function ($, spa, verify) {
    var model = {};

    model.init = function (m) {
        // console.log(m);
        $("#edit_submit").on('click', function () {
            //gvue.rebind()

            var obj = $("#edit_form *").serializeArray();
            var rule = {
                name: {
                    text: '收货人',
                    rules: 'required',
                },
                mobile: {
                    text: '手机号码',
                    verify_error: '验证失败',
                    rules: 'required|mobile',
                },
                // district: {
                //     text: '所在地区',
                //     require_error: '必须选择',
                //     rules: 'required',
                // },
                address: {
                    text: '详细地址',
                    rules: 'required',
                },
                // price: {
                //     text: '价格',
                //     require_error: '必须填写',
                //     verify_error: '验证失败',
                //     rules: 'required|regex',
                //     regex: /^[0-9]+$/
                // }
            }


            var flag = verify.run(obj, rule);
            if (flag) {
                var $url = root_path + '/user/address/post';
                $.ajax({
                    type: 'POST',
                    data: verify.arrayTojson(obj),
                    url: $url,
                    dataType: 'json',
                    beforeSend: function (xhr, settings) {
                        spa.showLoading();
                    },
                    success: function (json, status, xhr) {
                        spa.hideLoading();
                        mui.toast(json.msg);
                        if (json.flag) {
                            gvue.rebind()
                            spa.close('addressLocation,addressMap,addressEdit');
                        }
                    },
                    error: function (xhr, errorType, error) {
                        mui.toast('网络异常');
                        spa.hideLoading();
                    }
                })
            }


        })

    }

    return model
})